/**
 * 
 */
package neptune.manager.validator.rules.node;

import neptune.manager.model.topology.NetworkInterface;
import neptune.manager.model.topology.Node;
import neptune.manager.utils.Logger.MessageType;
import neptune.manager.validator.ValidatorResult;
import neptune.manager.validator.ValidatorRule;
import neptune.manager.validator.Validator.Status;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class ControlIfDefinitionRule extends ValidatorRule<Node> {

	@Override
	public String getName() {
		return "ControlIfDefinitionRule";
	}

	@Override
	protected ValidatorResult validationAlgorithm(Node toValidate,
			ValidatorResult result) {

		int controlIfNumber = 0;

		for (NetworkInterface netIf : toValidate.getInterfaces()) {
			if (netIf.getControlAddress() != null)
				controlIfNumber++;
		}

		if (controlIfNumber != 1) {
			result.setStatus(Status.ERROR);
			result.getLog().addMessage(
					"A node must have only one Control Interface. "
							+ "Number of control interface defined: "
							+ controlIfNumber, MessageType.ERROR);
		}

		return result;
	}

}
